package cn.wolfcode.realm;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.yaml.snakeyaml.events.Event;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class EmployeeRealm extends AuthorizingRealm {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private IRoleService roleService;
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
////        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
////        //查询当前登录用户
////        Employee employee = (Employee) principalCollection.getPrimaryPrincipal();
////        //查询寻用户权限集合
////        if (employee.isAdmin()) {
////            info.addStringPermission("*:*");
////        }else {
////            List<String> permissionList = permissionService.selEmployeePermissionById(employee.getId());
////                info.addStringPermissions(permissionList);
////            //查询当前用户的角色集合
////            List<Role> roleList = roleService.selEmployeeRoleById(employee.getId());
////            Set<String> roleSet = new HashSet<>();
////            for (Role role : roleList) {
////                roleSet.add(role.getSn());
////            }
////            info.addRoles(roleSet);
////        }
////        return info;
//
//    }
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//        Employee employee = (Employee) principalCollection.getPrimaryPrincipal();
//        if (employee.isAdmin()) {
//            info.addStringPermission("*:*");
//        }else {
//            //查询当前用户权限集合
//            List<String> permissionList = permissionService.selEmployeePermissionById(employee.getId());
//            info.addStringPermissions(permissionList);
//        }
//    }
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //获当前角色
        Employee employee = (Employee) principalCollection.getPrimaryPrincipal();

        if (employee.isAdmin()) {
            info.addStringPermission("*:*");
        } else {
            //获取当前用户权限信息
            List<String> permissionList = permissionService.selEmployeePermissionById(employee.getId());
            info.addStringPermissions(permissionList);

        }
        return info;
    }
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//        String username = (String) authenticationToken.getPrincipal();
//        Employee employee = employeeService.selectByUsername(username);
//        if (employee == null) {
//            return null;
//        } else {
//            return new SimpleAuthenticationInfo(employee, employee.getPassword(), ByteSource.Util.bytes(employee.getSalt()),getName());
//        }
        String username = (String) authenticationToken.getPrincipal();
        Employee employee = employeeService.selectByUsername(username);
        if (employee == null) {
            return null;
        }else {
            return new SimpleAuthenticationInfo(employee, employee.getPassword(),getName());
//            return new SimpleAuthenticationInfo(employee, employee.getPassword(), ByteSource.Util.bytes(employee.getSalt()), getName());
        }
    }
}
